package com.stx.friends.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.stx.friends.service.CommentService;
import com.stx.friends.service.UserService;
import com.stx.friends.vo.CommentVo;
import com.stx.friends.vo.UserVo;
	
public class UserAction extends ActionSupport{
	
	private UserService service ;
	private CommentService commentService;
	HttpServletRequest request=ServletActionContext.getRequest();
	HttpSession session=request.getSession();
	//关系信息列表
	private List<UserVo> userList = new ArrayList<UserVo>();
	
	//关系查询条件
	private UserVo criteria = null;
	
	//关系信息
	private UserVo user = null;
	
	//关系ID
	private String wrxUid = null;
	
	
	private String text=null;
	public String register(){
		
		if(service.getIdByUname(user.getUname()).equals("")){
			
			service.addUser(user);
			return SUCCESS;
		}	
		else{
			session.setAttribute("error", "用户已经存在，请重新输入用户名！");
			return INPUT;
		}
				
	}
	public String login(){
		
		UserVo uservo = service.findUserByUnameAndPassword(user.getUname(),user.getPassword());
		
		if (uservo != null) {
			session.setAttribute("wrxUid", service.getIdByUname(user.getUname()));
			session.setAttribute("username", user.getUname());
			
			session.setAttribute("nowuser", service.getUserById(service.getIdByUname(user.getUname())));
			
			session.setAttribute("users", service.list(uservo));
			session.setAttribute("comment", getCommentService().list(new CommentVo()));
			System.out.println(getCommentService().list(new CommentVo()).get(0).getWrxId());
			return "success";
		} else {
			addActionError("用户名或密码错误!");
			return INPUT;
		}
		
	}
	public String edit(){
		
		user=service.getUserById((String)session.getAttribute("wrxUid"));
		System.out.println("```1"+user.getPassword());
		return SUCCESS;
	}
	public String logout(){
		session.removeAttribute("wrxUid");
		return SUCCESS;
	}
	public String list() {

		userList = service.list(new UserVo());
		
		return "list";
	}
	public String update(){
		service.updateUser(criteria);
		
		return SUCCESS;
	}
	
	public String del(){
		
		//service.delUser(wrxUid);
	
		
		return "list";
	}
	
	public String add(){
		
		//service.addUser(user);
		
		return "list";
	}

	public void setService(UserService service) {
		this.service = service;
	}

	public List<UserVo> getUserList() {
		return userList;
	}

	public void setUserList(List<UserVo> userList) {
		this.userList = userList;
	}

	public UserVo getCriteria() {
		return criteria;
	}

	public void setCriteria(UserVo criteria) {
		this.criteria = criteria;
	}



	public UserVo getUser() {
		return user;
	}

	public void setUser(UserVo user) {
		this.user = user;
	}

	public String getWrxUid() {
		return wrxUid;
	}

	public void setWrxUid(String wrxUid) {
		this.wrxUid = wrxUid;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}
	public CommentService getCommentService() {
		return commentService;
	}
	public void setCommentService(CommentService commentService) {
		this.commentService = commentService;
	}
	
}
